package org.eenie.commonlib.ui.arch


import android.content.Context
import androidx.lifecycle.rxLifeScope
import co.zsmb.rainbowcake.base.RainbowCakeViewModel
import com.blankj.utilcode.util.LogUtils
import com.blankj.utilcode.util.ToastUtils
import com.qmuiteam.qmui.widget.dialog.QMUITipDialog
import kotlinx.coroutines.delay


import javax.inject.Inject

class ExampleViewModel @Inject constructor(private val presenter: ExamplePresenter, private val context: Context) : RainbowCakeViewModel<ExampleViewState>(ExampleViewState()) {
    private var mCount = 0
    private val mDialog by lazy {
        QMUITipDialog.Builder(context)
                .setIconType(QMUITipDialog.Builder.ICON_TYPE_LOADING)
                .setTipWord("正在加载...")
                .create()
    }


    fun load() = executeCancellable {
        mCount++
        LogUtils.e(context)
        delay(3000)
        viewState = viewState.copy().apply {
            name = presenter.getUserInfo()
            LogUtils.e(name)
        }
    }


    fun loadUserInfo() {
        rxLifeScope.launch({
            delay(3000)
            viewState = viewState.copy().apply {
                name = presenter.getUserInfo()
                LogUtils.e(name)
            }
        }, {
            it.printStackTrace()
            ToastUtils.showShort(it.message)
        }, {
            LogUtils.e("start")
            mDialog.show()
        }, {
            LogUtils.e("final")
            mDialog.dismiss()
        })
    }

}
